#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/3/14 15:28
# @Author  : Swper
# @Site    : 
# @File    : models.py
# @Software: PyCharm
from app.exts import db
from datetime import datetime
from werkzeug.security import check_password_hash,generate_password_hash

#用户表
class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50),unique=True,nullable=False)
    fullname = db.Column(db.String(50),default=None)
    mobile = db.Column(db.String(11),unique=True,default=None)
    pwd = db.Column(db.String(255),nullable=False)
    email = db.Column(db.String(100),nullable=False)
    is_admin = db.Column(db.SmallInteger,default='0')  #'是否是超级管理员 1表示是 0 表示不是'
    status = db.Column(db.SmallInteger,default='1')  #状态 1：有效 0：无效
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)  #最后更新时间
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间


    def __repr__(self):
        return "<User %r>" % self.name

#拦截用户密码，进行加密后再存入数据库
    def __init__(self,*args,**kwargs):
        name = kwargs.get('name')
        fullname = kwargs.get('fullname')
        mobile = kwargs.get('mobile')
        pwd = kwargs.get('pwd')
        email = kwargs.get('email')

        self.name = name
        self.fullname = fullname
        self.mobile = mobile
        self.email = email
        self.pwd = generate_password_hash(pwd)

    def check_password(self,raw_password):
        result = check_password_hash(self.pwd,raw_password)
        return result


#角色表
class Role(db.Model):
    __tablename__ = 'role'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50),unique=True,nullable=False)
    status = db.Column(db.SmallInteger,default='1')  #状态 1：有效 0：无效
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)  #最后更新时间
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间

    def __repr__(self):
        return "<Role %r>" % self.name



class User_role(db.Model):
    __tablename__ = 'user_role'      #用户角色表
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    uid = db.Column(db.Integer,db.ForeignKey('user.id'))
    role_id = db.Column(db.Integer,db.ForeignKey('role.id'))
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间

    def __repr__(self):
        return "<User_role %r>" % self.id


#权限详情表
class Access(db.Model):
    __tablename__ = 'access'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title = db.Column(db.String(100),nullable=False,default='')
    urls = db.Column(db.String(1000),nullable=False,default='')
    status = db.Column(db.SmallInteger,default='1')  #状态 1：有效 0：无效
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)  #最后更新时间
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间

    def __repr__(self):
        return "<Access %r>" % self.title


#角色权限表
class Role_access(db.Model):
    __tablename__ = 'role_access'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    role_id = db.Column(db.Integer,default=None)
    access_id = db.Column(db.Integer,default=None)

    def __repr__(self):
        return "<Role_access %r>" % self.id


# class Access_log(db.Model):
#     __tablename__ ='access_log'    #用户操作记录表
#     id = db.Column(db.Integer,primary_key=True,autoincrement=True)
#     uid = db.Column(db.Integer,db.ForeignKey('user.id'))
#     target_url = db.Column(db.String(255),nullable=False,default='')
#     query_params = db.Column(db.Text,nullable=False,default='')
#     ua = db.Column(db.String(255),nullable=False,default='')
#     ip = db.Column(db.String(32),nullable=False,default='')
#     note = db.Column(db.String(255),nullable=False,default='')
#     create_time = db.Column(db.DateTime,index=True,nullable=False,default='0000-00-00 00:00:00')  #创建时间
#
#     def __repr__(self):
#         return "<Access_log %r>" % self.id
#
#
#     #
#资产组
class AssetsGroup(db.Model):
    __tablename__ = 'assetsgroup'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50),unique=True)
    status = db.Column(db.SmallInteger,default='1')  #状态 1：有效 0：无效
    update_time = db.Column(db.DateTime,index=True,default=datetime.now)  #最后更新时间
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间

    def __repr__(self):
        return "<AssetsGroup %r>" % self.name


#资产列表
class Assets(db.Model):
    __tablename__ = 'assets'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    hostname = db.Column(db.String(100),unique=True)
    ostype = db.Column(db.Integer)
    privateIp = db.Column(db.String(32),unique=True)
    publicIp = db.Column(db.String(32),unique=True)
    ports = db.Column(db.String(255))
    diskinfo = db.Column(db.String(255))
    meminfo = db.Column(db.String(255))
    grouptype = db.Column(db.Integer)
    status = db.Column(db.SmallInteger,default='1')  #状态 1：有效 0：无效
    comment = db.Column(db.String(255))
    update_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 最后更新时间
    create_time = db.Column(db.DateTime, index=True, default=datetime.now)  # 创建时间

    def __repr__(self):
        return "<Assets %r>" % self.id


#业务分组
class GroupType(db.Model):
    __tablename__ = 'grouptype'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(50),unique=True)
    create_time = db.Column(db.DateTime,index=True,default=datetime.now)  #创建时间

    def __repr__(self):
        return "<GroupType %r>" % self.name




